var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "itemId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#witapp',
    data:{
        showList: true,
        title: null,
        item:{
            parentName:null,
            parentId:0,
            type:1,
            orderNum:0
        }
    },
    methods: {
        getItem: function(itemId){
            //加载菜单树
            $.get(baseURL + "sys/item/select", function(r){
                ztree = $.fn.zTree.init($("#itemTree"), setting, r.itemList);
                var node = ztree.getNodeByParam("itemId", vm.item.parentId);
                ztree.selectNode(node);
                vm.item.parentName = node.name;
            })
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.item = {parentName:null,parentId:0,type:1,orderNum:0};
            vm.getItem();
        },
        update: function () {
            var itemId = getItemId();
            if(itemId == null){
                return ;
            }

            $.get(baseURL + "sys/item/info/"+itemId, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.item = r.item;

                vm.getItem();
            });
        },
        del: function () {
            var itemId = getItemId();
            if(!itemId){
                alert('请选择要删除的数据!');
                return;
            }
            confirm('确定要删除选中的记录？',function(){
                ajaxExecute(baseURL + "sys/item/delete?itemId=" + itemId,{},function(data){
                    if(data.code == 0){
                        vm.reload();
                    }else{
                        alert(data.msg);
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }
            var url = vm.item.itemId == null ? "sys/item/save" : "sys/item/update";
            $.ajax({
                type: "POST",
                url:  baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.item),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        itemTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择菜单",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#itemLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.item.parentId = node[0].itemId;
                    vm.item.parentName = node[0].name;

                    layer.close(index);
                }
            });
        },
        reload: function () {
            vm.showList = true;
            Item.table.refresh();
        },
        validator: function () {
            if(isBlank(vm.item.name)){
                alert("字典名称不能为空");
                return true;
            }
            //编码
            if(isBlank(vm.item.code)){
                alert("字典编码不能为空");
                return true;
            }
        }
    }
});

var Item = {
    id: "itemTable",
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Item.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '字典ID', hidden:true, field: 'itemId', align: 'center', valign: 'middle', width: '80px'},
        {title: '字典名称', field: 'name', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '字典编码', field: 'code', align: 'center', valign: 'middle', sortable: true, width: '160px'},
        {title: '上级字典', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '排序号', field: 'orderNum', align: 'center', valign: 'middle', sortable: true, width: '100px'}]
    return columns;
};


function getItemId () {
    var selected = $('#itemTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return false;
    } else {
        return selected[0].id;
    }
}

$(function () {
    var colunms = Item.initColumn();
    var table = new TreeTable(Item.id, baseURL + "sys/item/list", colunms);
    table.setExpandColumn(2);
    table.setIdField("itemId");
    table.setCodeField("itemId");
    table.setParentCodeField("parentId");
    table.setExpandAll(false);
    table.init();
    Item.table = table;
});
